package com.lagou.web.servlet;

import com.alibaba.fastjson.JSON;
import com.lagou.base.BaseServlet;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.service.Impl.CourseServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/course")
public class CourseServlet extends BaseServlet {
    public void findCourseList(HttpServletRequest req, HttpServletResponse resp) {
        try {
            CourseService cs = new CourseServiceImpl();
            List<Course> courseList = cs.findCourseList();
            String s = JSON.toJSONString(courseList);

            resp.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        public void findCourseNameAndStatus(HttpServletRequest req, HttpServletResponse resp){

         try{
             //1.接收参数
            String course_name=  req.getParameter("course_name");
            String status = req.getParameter("status");
            //2.业务处理
             CourseService courseService=   new CourseServiceImpl();
             List<Course> list= courseService.findCourseNameAndStatus(course_name,status);

             String s =JSON.toJSONString(list);

                resp.getWriter().print(s);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        //id查询
    public void findCourseById(HttpServletRequest req, HttpServletResponse resp){

        try{
        //获取客户端ID
        String id = req.getParameter("id");
        int i = Integer.parseInt(id);
        CourseService cs = new CourseServiceImpl();
        List<Course> list = cs.findCourseById(i);
        //对象转JSON
        String s = JSON.toJSONString(list);

            resp.getWriter().print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
